#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QThread>
#include<QMessageBox>
#include<QFileDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MySocket *socked=new MySocket;
    QThread *thread=new QThread(this);
    ui->Port->setText("8899");
    ui->IP->setText("127.0.0.1");
    socked->moveToThread(thread);
    ui->prograssbar->setValue(0);
    ui->prograssbar->setRange(0,100);


    connect(this,&MainWindow::sendtoSocket,socked,&MySocket::connectSever);
    connect(socked,&MySocket::sendConnect,this,[=](){

    QMessageBox::information(this,"连接服务器","恭喜你连接成功");

    });
    connect(socked,&MySocket::gameover,this,[=](){
        thread->quit();
        thread->wait();
        thread->deleteLater();


        socked->deleteLater();

    });

    connect(this,&MainWindow::sendFilePath,socked,&MySocket::sendFile);
    connect(socked,&MySocket::sendFileProcess,ui->prograssbar,&QProgressBar::setValue);
    thread->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

//悬着文件
void MainWindow::on_selFile_clicked()
{
    QString path=QFileDialog::getOpenFileName(this, tr("Open File"),
                                 "../");
    ui->path->setText(path);
}
//发送那个文件
void MainWindow::on_sendFile_clicked()
{
   QString filepath=ui->path->text();
   emit sendFilePath(filepath);
}
//连接服务器
void MainWindow::on_connect_clicked()
{
    QString IP=ui->IP->text();
    QString  port=ui->Port->text();
    quint16 port1=port.toInt();
    emit sendtoSocket(IP,port1);
}
